package com.example.hotel.service;

import com.example.hotel.dto.checkOut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.hotel.dto.Room;
import com.example.hotel.dto.checkIn;
import com.example.hotel.dto.roomStatus;
import com.example.hotel.mapper.hotelMapper;

@Service
public class hotelService {

    private final hotelMapper hotelMapper;
    @Autowired
    public hotelService(hotelMapper hotelMapper) {
        this.hotelMapper = hotelMapper;
    }

    // 办理入住
    public boolean checkIn(checkIn request) {
        roomStatus roomStatus = hotelMapper.getRoomStatus(request.getRoomNumber());
        if (roomStatus != null && "空闲".equals(roomStatus.getStatus())) {
            hotelMapper.insertCheckIn(request);
            hotelMapper.updateRoomStatus(request.getRoomNumber(), "占用");
            return true;
        }
        return false;
    }

    // 退房
    public boolean checkOut(checkOut request) {
        roomStatus roomStatus = hotelMapper.getRoomStatus(request.getRoomNumber());
        if (roomStatus != null && "占用".equals(roomStatus.getStatus())) {
            hotelMapper.deleteCheckInByRoomNumber(request.getRoomNumber());
            hotelMapper.updateRoomStatus(request.getRoomNumber(), "空闲");
            hotelMapper.updateRoomNeedCleaning(request.getRoomNumber(), true);
            return true;
        }
        return false;
    }
    public boolean checkOut(int roomNumber) {
        roomStatus roomStatus = hotelMapper.getRoomStatus(roomNumber);
        if (roomStatus != null && "占用".equals(roomStatus.getStatus())) {
            hotelMapper.deleteCheckInByRoomNumber(roomNumber);
            hotelMapper.updateRoomStatus(roomNumber, "空闲");
            hotelMapper.updateRoomNeedCleaning(roomNumber, true);
            return true;
        }
        return false;
    }

    // 打扫房间
    public boolean markRoomClean(int roomNumber, boolean isClean) {
        roomStatus roomStatus = hotelMapper.getRoomStatus(roomNumber);
        if (roomStatus != null) {
            hotelMapper.updateRoomCleaningStatus(roomNumber, isClean);
            return true;
        }
        return false;
    }

    // 延迟退房
    public boolean extendCheckOut(int roomNumber, String newEndTime) {
        roomStatus roomStatus = hotelMapper.getRoomStatus(roomNumber);
        if (roomStatus != null && "占用".equals(roomStatus.getStatus())) {
            hotelMapper.updateCheckInEndTime(roomNumber, newEndTime);
            return true;
        }
        return false;
    }

    //获取Wifi密码
    public Room getWifiPassWord(int roomNumber){
        Room room = hotelMapper.getWifiPassWord(roomNumber);
        return room;
    }

    
}
